package com.bestcem.xm.ticket.service.manager.notify;

import com.bestcem.xm.ticket.enums.TicketNotifyConfigEventEnum;
import com.bestcem.xm.ticket.grpc.client.TicketMessageCenterGrpcService;
import com.bestcem.xm.ticket.grpc.client.enums.messagecenter.TemplateScenarioEnum;
import com.bestcem.xm.ticket.grpc.client.enums.messagecenter.TemplateTypeEnum;
import com.bestcem.xm.ticket.service.manager.notify.dto.NotifyMessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.bestcem.xm.ticket.service.manager.notify.FiledConstant.Email.*;

/**
 * 工单通知模板 工具类
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/11/24 10:32
 */
@Slf4j
@Component
public class TemplateManager {

    @Resource
    private TicketMessageCenterGrpcService ticketMessageCenterGrpcService;

    private static final Map<TicketNotifyConfigEventEnum, TemplateScenarioEnum> NOTIFY_EVENT_SCENARIO_MAP = new HashMap<>();

    static {
        NOTIFY_EVENT_SCENARIO_MAP.put(TicketNotifyConfigEventEnum.CREATED_TICKET, TemplateScenarioEnum.RECEIVE_ORDER);
        NOTIFY_EVENT_SCENARIO_MAP.put(TicketNotifyConfigEventEnum.FOLLOW_REMIND, TemplateScenarioEnum.ORDER_OVERDUE_AGO);
        NOTIFY_EVENT_SCENARIO_MAP.put(TicketNotifyConfigEventEnum.FOLLOW_OVERDUE, TemplateScenarioEnum.ORDER_OVERDUE);
        NOTIFY_EVENT_SCENARIO_MAP.put(TicketNotifyConfigEventEnum.RECEIVE_AUDIT, TemplateScenarioEnum.RECEIVE_ORDER);
        NOTIFY_EVENT_SCENARIO_MAP.put(TicketNotifyConfigEventEnum.AUDIT_REMIND, TemplateScenarioEnum.ORDER_OVERDUE_AGO);
        NOTIFY_EVENT_SCENARIO_MAP.put(TicketNotifyConfigEventEnum.AUDIT_OVERDUE, TemplateScenarioEnum.ORDER_OVERDUE);
        NOTIFY_EVENT_SCENARIO_MAP.put(TicketNotifyConfigEventEnum.CHANGE_FOLLOW, TemplateScenarioEnum.RECEIVE_ORDER);
        NOTIFY_EVENT_SCENARIO_MAP.put(TicketNotifyConfigEventEnum.TICKETS_STATISTICS, TemplateScenarioEnum.RECEIVE_ORDER);
        NOTIFY_EVENT_SCENARIO_MAP.put(TicketNotifyConfigEventEnum.ONLY_NOTIFY, TemplateScenarioEnum.RECEIVE_WARNING);
    }


    /**
     * 获取工单通知默认模板
     *
     * @param orgId 公司id
     * @param event 通知事件 {@link TicketNotifyConfigEventEnum}
     * @param type  模板类型 {@link TemplateTypeEnum}
     * @return 通知模板
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/11/24 10:59
     */
    @Nonnull
    public NotifyMessageDTO getDefaultTemplate(String orgId, TicketNotifyConfigEventEnum event, TemplateTypeEnum type) {
        NotifyMessageDTO defaultTemplate = ticketMessageCenterGrpcService.getDefaultTemplate(orgId, NOTIFY_EVENT_SCENARIO_MAP.get(event), type);
        if (log.isInfoEnabled()) {
            log.info("[Ticket] Notify getDefaultTemplate {}-{}-{}-{}", orgId, event, type, defaultTemplate);
        }
        if (Objects.isNull(defaultTemplate)) {
            defaultTemplate = new NotifyMessageDTO();
            defaultTemplate.setSubject(NOTIFY_EVENT_SUBJECT_MAP.get(event));
            defaultTemplate.setContent(NOTIFY_EVENT_CONTENT_MAP.get(event));
        }
        return defaultTemplate;
    }


    /**
     * 工单创建
     */
    private static final String CREATE_TICKET_SUBJECT = "[" + PROJECT_NAME + "]收到新的工单";
    private static final String CREATE_TICKET_CONTENT =
            "[" + PROJECT_NAME + "]收到新的工单，请登录[" + SYSTEM_NAME + "]查看。<br>\n" +
                    "工单编号：" + TICKET_NO + "<br>\n" +
                    "工单链接：" + URL + "<br>\n" +
                    "工单生成时间：" + CREATE_TIME + "<br>\n" +
                    "处理逾期时间：" + FOLLOW_OVERDUE_TIME + "<br>\n" +
                    "触发规则：" + RULE_NAME + "<br>";
    /**
     * 处理逾期前提醒
     */
    private static final String FOLLOW_OVERDUE_REMIND_SUBJECT = "[" + PROJECT_NAME + "]工单处理即将逾期";
    private static final String FOLLOW_OVERDUE_REMIND_CONTENT =
            "[" + PROJECT_NAME + "]工单处理即将逾期，请登录[" + SYSTEM_NAME + "]查看。<br>\n" +
                    "工单编号：" + TICKET_NO + "<br>\n" +
                    "工单链接：" + URL + "<br>\n" +
                    "工单生成时间：" + CREATE_TIME + "<br>\n" +
                    "处理逾期时间：" + FOLLOW_OVERDUE_TIME + "<br>\n" +
                    "触发规则：" + RULE_NAME + "<br>";
    /**
     * 处理已逾期
     */
    private static final String FOLLOW_OVERDUE_SUBJECT = "[" + PROJECT_NAME + "]工单处理逾期";
    private static final String FOLLOW_OVERDUE_CONTENT =
            "[" + PROJECT_NAME + "]以下工单已经处理逾期，请登录[" + SYSTEM_NAME + "]查看。<br>\n" +
                    "工单编号：" + TICKET_NO + "<br>\n" +
                    "工单链接：" + URL + "<br>\n" +
                    "工单生成时间：" + CREATE_TIME + "<br>\n" +
                    "处理逾期时间：" + FOLLOW_OVERDUE_TIME + "<br>\n" +
                    "触发规则：" + RULE_NAME + "<br>";
    /**
     * 收到审核
     */
    private static final String AUDITING_SUBJECT = "[" + PROJECT_NAME + "]收到新的工单审核";
    private static final String AUDITING_CONTENT =
            "[" + PROJECT_NAME + "]收到新的工单审核，请登录[" + SYSTEM_NAME + "]查看。<br>\n" +
                    "工单编号：" + TICKET_NO + "<br>\n" +
                    "工单链接：" + URL + "<br>\n" +
                    "工单生成时间：" + CREATE_TIME + "<br>\n" +
                    "审核逾期时间：" + FOLLOW_OVERDUE_TIME + "<br>";
    /**
     * 审核逾期前提醒
     */
    private static final String AUDIT_OVERDUE_REMIND_SUBJECT = "[" + PROJECT_NAME + "]工单审核即将逾期";
    private static final String AUDIT_OVERDUE_REMIND_CONTENT =
            "[" + PROJECT_NAME + "]以下工单审核即将逾期，请登录[" + SYSTEM_NAME + "]查看。<br>\n" +
                    "工单编号：" + TICKET_NO + "<br>\n" +
                    "工单链接：" + URL + "<br>\n" +
                    "工单生成时间：" + CREATE_TIME + "<br>\n" +
                    "审核逾期时间：" + FOLLOW_OVERDUE_TIME + "<br>";
    /**
     * 审核已逾期
     */
    private static final String AUDIT_OVERDUE_SUBJECT = "[" + PROJECT_NAME + "]工单审核逾期";
    private static final String AUDIT_OVERDUE_CONTENT =
            "[" + PROJECT_NAME + "]以下工单审核逾期，请登录[" + SYSTEM_NAME + "]查看。<br>\n" +
                    "工单编号：" + TICKET_NO + "<br>\n" +
                    "工单链接：" + URL + "<br>\n" +
                    "工单生成时间：" + CREATE_TIME + "<br>\n" +
                    "审核逾期时间：" + FOLLOW_OVERDUE_TIME + "<br>";
    /**
     * 转派处理
     */
    private static final String TRANSFER_FOLLOW_SUBJECT = "[" + PROJECT_NAME + "]收到新的工单";
    private static final String TRANSFER_FOLLOW_CONTENT =
            "[" + PROJECT_NAME + "]收到新的工单，请登录[" + SYSTEM_NAME + "]查看。<br>\n" +
                    "工单编号：" + TICKET_NO + "<br>\n" +
                    "工单链接：" + URL + "<br>\n" +
                    "工单生成时间：" + CREATE_TIME + "<br>\n" +
                    "处理逾期时间：" + FOLLOW_OVERDUE_TIME + "<br>\n" +
                    "触发规则：" + RULE_NAME + "<br>";
    /**
     * 工单仅通知模板 (预警)
     */
    private static final String ONLY_NOTIFY_SUBJECT = "[" + PROJECT_NAME + "]收到新的预警";
    private static final String ONLY_NOTIFY_CONTENT =
            "[" + PROJECT_NAME + "]收到新的预警，请登录[" + SYSTEM_NAME + "]查看。<br>\n" +
                    "预警编号：" + TICKET_NO + "<br>\n" +
                    "预警链接：" + URL + "<br>\n" +
                    "预警生成时间：" + CREATE_TIME + "<br>\n" +
                    "触发规则：" + RULE_NAME + "<br>";


    private static final Map<TicketNotifyConfigEventEnum, String> NOTIFY_EVENT_SUBJECT_MAP = new HashMap<>();

    static {
        NOTIFY_EVENT_SUBJECT_MAP.put(TicketNotifyConfigEventEnum.CREATED_TICKET, CREATE_TICKET_SUBJECT);
        NOTIFY_EVENT_SUBJECT_MAP.put(TicketNotifyConfigEventEnum.FOLLOW_REMIND, FOLLOW_OVERDUE_REMIND_SUBJECT);
        NOTIFY_EVENT_SUBJECT_MAP.put(TicketNotifyConfigEventEnum.FOLLOW_OVERDUE, FOLLOW_OVERDUE_SUBJECT);
        NOTIFY_EVENT_SUBJECT_MAP.put(TicketNotifyConfigEventEnum.RECEIVE_AUDIT, AUDITING_SUBJECT);
        NOTIFY_EVENT_SUBJECT_MAP.put(TicketNotifyConfigEventEnum.AUDIT_REMIND, AUDIT_OVERDUE_REMIND_SUBJECT);
        NOTIFY_EVENT_SUBJECT_MAP.put(TicketNotifyConfigEventEnum.AUDIT_OVERDUE, AUDIT_OVERDUE_SUBJECT);
        NOTIFY_EVENT_SUBJECT_MAP.put(TicketNotifyConfigEventEnum.CHANGE_FOLLOW, TRANSFER_FOLLOW_SUBJECT);
        NOTIFY_EVENT_SUBJECT_MAP.put(TicketNotifyConfigEventEnum.TICKETS_STATISTICS, CREATE_TICKET_SUBJECT);
        NOTIFY_EVENT_SUBJECT_MAP.put(TicketNotifyConfigEventEnum.ONLY_NOTIFY, ONLY_NOTIFY_SUBJECT);
    }

    private static final Map<TicketNotifyConfigEventEnum, String> NOTIFY_EVENT_CONTENT_MAP = new HashMap<>();

    static {
        NOTIFY_EVENT_CONTENT_MAP.put(TicketNotifyConfigEventEnum.CREATED_TICKET, CREATE_TICKET_CONTENT);
        NOTIFY_EVENT_CONTENT_MAP.put(TicketNotifyConfigEventEnum.FOLLOW_REMIND, FOLLOW_OVERDUE_REMIND_CONTENT);
        NOTIFY_EVENT_CONTENT_MAP.put(TicketNotifyConfigEventEnum.FOLLOW_OVERDUE, FOLLOW_OVERDUE_CONTENT);
        NOTIFY_EVENT_CONTENT_MAP.put(TicketNotifyConfigEventEnum.RECEIVE_AUDIT, AUDITING_CONTENT);
        NOTIFY_EVENT_CONTENT_MAP.put(TicketNotifyConfigEventEnum.AUDIT_REMIND, AUDIT_OVERDUE_REMIND_CONTENT);
        NOTIFY_EVENT_CONTENT_MAP.put(TicketNotifyConfigEventEnum.AUDIT_OVERDUE, AUDIT_OVERDUE_CONTENT);
        NOTIFY_EVENT_CONTENT_MAP.put(TicketNotifyConfigEventEnum.CHANGE_FOLLOW, TRANSFER_FOLLOW_CONTENT);
        NOTIFY_EVENT_CONTENT_MAP.put(TicketNotifyConfigEventEnum.TICKETS_STATISTICS, CREATE_TICKET_CONTENT);
        NOTIFY_EVENT_CONTENT_MAP.put(TicketNotifyConfigEventEnum.ONLY_NOTIFY, ONLY_NOTIFY_CONTENT);
    }
}
